package infos

import (
	"fmt"
	"jgt-sdk/oper"
	"strconv"
	"strings"
	"time"

	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/app/sysparam"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/groupd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/log"

	"github.com/gin-gonic/gin"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/columnd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"
)

type respCarouse struct {
	Image string `json:"image"`
	URL   string `json:"url"`
}

type respCommonInfo struct {
	Title  string     `json:"title"`
	Detail string     `json:"detail"`
	Tags   []string   `json:"tags,omitempty"`
	Date   *time.Time `json:"date,omitempty"`
	Images []string   `json:"images"`
	URL    string     `json:"url"`
	Type   int        `json:"type"`
	ID     string     `json:"id"`
}

// 组信息转换响应适配器
func adapterG2Resp(ctx *gin.Context, v *infod.InfoAggregate, g *groupd.GroupInfoEntity, col *columnd.ColumnAggregate) interface{} {
	return _adapterResp(ctx, v, col, func() string {
		return g.Seq
	})
}

// 信息转换响应适配器
func adapterC2Resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate) interface{} {
	return _adapterResp(ctx, v, col, func() string {
		return v.InfoID
	})
}

// 信息转换响应适配器 指定适配器
func adapterC2RespForCode(ctx *gin.Context, show string, v *infod.InfoAggregate, col *columnd.ColumnAggregate) interface{} {
	return _adapterRespForAdapter(ctx, show, v, col, func() string {
		return v.InfoID
	})
}

// 栏目信息转换响应适配器
func _adapterResp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, seqhandler func() string) interface{} {
	show := ctx.Param("show")
	if show == "" {
		show = showModeAppCommon
	}
	return _adapterRespForAdapter(ctx, show, v, col, seqhandler)
}

// 栏目信息转换响应适配器
func _adapterRespForAdapter(ctx *gin.Context, show string, v *infod.InfoAggregate, col *columnd.ColumnAggregate, seqhandler func() string) interface{} {
	url := col.Infourl
	if url == "" {
		url = sysparam.ColumnDetailURL()
	}
	url = strings.Replace(url, ":infoid", v.InfoID, -1)
	if ad, ok := adapter[show]; ok {
		return ad(ctx, v, col, url, seqhandler)
	}
	return data2resp(ctx, v, col, url, seqhandler)
}

type adapterHandler func(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{}

var adapter map[string]adapterHandler

func init() {
	adapter = make(map[string]adapterHandler)
	adapter[showModeAppCommon] = common2resp
	adapter[showModeNoTag] = notag2resp
	adapter[showModeCarouse] = carouse2resp
	adapter[showModeData] = data2resp
	adapter[showModeZsxx] = zsxx2resp
	adapter[showModeZlxx] = zlxx2resp
	adapter[showModeZpxx] = zpxx2resp
	adapter[showModeSearch] = sch2resp
	adapter[showModeMyInfo] = myInfos2resp

}
func carouse2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	image := v.Banner
	if len(v.Icon) > 0 {
		image = v.Icon[0]
	}
	return respCarouse{
		URL:   url,
		Image: image,
	}
}
func common2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	t := v.PublishTime
	var out = respCommonInfo{
		Title:  v.Title,
		Detail: v.Desc,
		Tags:   v.Tags,
		Date:   &t,
		Images: v.Icon,
		URL:    url,
		Type:   v.ListMode,
		ID:     seqhandler(),
	}
	if out.Type == 0 {
		out.Type = 1
	}
	if v.ListMode == 4 {
		out.Images = []string{v.Banner}
	}
	return out
}
func notag2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	t := v.PublishTime
	var out = respCommonInfo{
		Title:  v.Title,
		Detail: v.Desc,
		Date:   &t,
		Images: v.Icon,
		URL:    url,
		Type:   v.ListMode,
		ID:     seqhandler(),
	}
	if out.Type == 0 {
		out.Type = 1
	}
	if v.ListMode == 4 {
		out.Images = []string{v.Banner}
	}
	return out
}
func zsxx2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	tags := []string{}
	if tmp, ok := v.Extras["fl"]; ok {
		if fl, ok := tmp.(string); ok {
			tags = append(tags, fl)
		}
	}

	if tmp, ok := v.Extras["zsfs"]; ok {
		if zsfs, ok := tmp.(string); ok {
			tags = append(tags, zsfs)
		}
	}

	var out = respCommonInfo{
		Title:  v.Title,
		Detail: v.Desc,
		Tags:   tags,
		Date:   nil,
		Images: v.Icon,
		URL:    url,
		Type:   v.ListMode,
		ID:     seqhandler(),
	}
	if out.Type == 0 {
		out.Type = 1
	}
	if v.ListMode == 4 && v.Banner != "" {
		out.Images = []string{v.Banner}
	}
	return out
}
func zlxx2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	tags := []string{}

	fl := ""
	if tmp, ok := v.Extras["fl"]; ok {
		if fl1, ok := tmp.(string); ok {
			tags = append(tags, fl1)
			fl = fl1
		}
	}

	if tmp, ok := v.Extras["fybq"]; ok {
		if fybq, ok := tmp.(string); ok {
			tags = append(tags, fybq)
		}
	}
	fydz := v.Extras["fydz"]
	mj := v.Extras["mj"]
	zj := v.Extras["zj"]
	desc := ""
	if fl == "写字楼" {
		desc = fmt.Sprintf("%v\n租金:%v元/m²/天 面积:%vm²", fydz, mj, zj)
	} else if fl == "住宅" {
		desc = fmt.Sprintf("%v\n租金:%v元/月 面积:%vm²", fydz, mj, zj)
	}

	var out = respCommonInfo{
		Title:  v.Title,
		Detail: desc,
		Tags:   tags,
		Images: v.Icon,
		URL:    url,
		Type:   v.ListMode,
		ID:     seqhandler(),
	}
	if out.Type == 0 {
		out.Type = 1
	}
	if v.ListMode == 4 {
		out.Images = []string{v.Banner}
	}
	return out
}

type respZpxx struct {
	ID       string    `json:"id"`       // "排序标识"
	Post     string    `json:"post"`     // "岗位名称",
	Money    string    `json:"money"`    // "薪资范围",
	Company  string    `json:"company"`  // "公司名称",
	Location string    `json:"location"` // "公司所在地",
	Tags     []string  `json:"tags"`     //  ["标签",  "标签"],
	Date     time.Time `json:"date"`     //  时间格式  RFC3339
	Icon     string    `json:"icon"`     // "公司logo地址"
	URL      string    `json:"url"`      // "链接地址"
}

func zpxx2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	tags := []string{}

	gznx := extrasForString(v.Extras, "gznx")
	xl := extrasForString(v.Extras, "xl")
	if gznx != "" {
		tags = append(tags, gznx)
	}
	if xl != "" {
		tags = append(tags, xl)
	}
	var out = respZpxx{
		ID:       seqhandler(),
		Post:     v.Title,
		Money:    extrasForString(v.Extras, "xz"),
		Tags:     tags,
		Company:  "",
		Location: "",
		Icon:     "",
		Date:     v.PublishTime,
		URL:      url,
	}
	if v.Org == "" {
		return out
	}
	if enter, err := oper.GetHandle().GetEnterprise(v.Org[4:]); err == nil {
		out.Company = enter.Name
		out.Location = enter.Address
		out.Icon = enter.Logo
	} else {
		log.Warnf("获取企业信息失败:%v", err)
	}
	return out
}
func extrasForString(extras map[string]interface{}, key string) string {
	tmp := extras[key]
	if tmp != nil {
		return tmp.(string)
	}
	return ""
}
func data2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	showctt := getIntByQuery(ctx, "ctt", 0)
	switch col.InfoMode {
	case columnd.InfoModeSingle:
		showctt = 1
	}
	content := ""
	if showctt == 1 {
		content = v.Content
	}

	return respInfo{
		InfoID:      v.InfoID,
		ColumnID:    v.ColumnID,
		ColumnName:  col.Name,
		Seq:         seqhandler(),
		Kind:        v.Kind,
		Title:       v.Title,
		SubTitle:    v.SubTitle,
		ListMode:    v.ListMode,
		Icon:        v.Icon,
		Banner:      v.Banner,
		Desc:        v.Desc,
		Tags:        v.Tags,
		Extras:      v.Extras,
		PublishTime: v.PublishTime,
		Creator:     v.Creator,
		Owner:       v.Owner,
		Org:         v.Org,
		Content:     content,
		URL:         url,
	}

}
func myInfos2resp(ctx *gin.Context, v *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	showctt := getIntByQuery(ctx, "ctt", 0)
	switch col.InfoMode {
	case columnd.InfoModeSingle:
		showctt = 1
	}
	content := ""
	if showctt == 1 {
		content = v.Content
	}
	t := v.CreTime
	return respInfo{
		InfoID:      v.InfoID,
		ColumnID:    v.ColumnID,
		ColumnName:  col.Name,
		Seq:         seqhandler(),
		Kind:        v.Kind,
		Title:       v.Title,
		SubTitle:    v.SubTitle,
		ListMode:    v.ListMode,
		Icon:        v.Icon,
		Banner:      v.Banner,
		Desc:        v.Desc,
		Tags:        v.Tags,
		Extras:      v.Extras,
		PublishTime: v.PublishTime,
		CreTime:     &t,
		Creator:     v.Creator,
		Owner:       v.Owner,
		Org:         v.Org,
		Content:     content,
		URL:         url,
		Status:      int(v.Status),
	}

}
func getIntByQuery(ctx *gin.Context, key string, defaultValue int) int {
	v := ctx.Query(key)
	if v == "" {
		return defaultValue
	}
	if i, err := strconv.Atoi(v); err == nil {
		return i
	}
	return defaultValue
}

type schResp struct {
	Seq   string   `json:"seq"`   // 排序序号
	Title string   `json:"title"` // 标题
	Tags  []string `json:"tags"`  // 标签
	URL   string   `json:"url"`   // 连接
}

func sch2resp(ctx *gin.Context, i *infod.InfoAggregate, col *columnd.ColumnAggregate, url string, seqhandler func() string) interface{} {
	tags := make([]string, 0, 3)
	if len(tags) == 0 {
		if col.ShortName != "" {

			tags = append(tags, col.ShortName)
		} else {
			tags = append(tags, col.Name)
		}
	}
	if len(i.Tags) > 0 {
		tags = append(tags, i.Tags...)
	}
	return schResp{
		Seq:   seqhandler(),
		Title: i.Title,
		Tags:  tags,
		URL:   url,
	}
}
